"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : explain table
Case Name   : 验证原有取值TEXT,XML,YAML写成format=format_type的格式，取值不加引号,不区分大小写
Create At   : 2023-01-11
@zou_jialiang0501162244
Description :
    1.兼容B库建表并插入数据
    2.format=text格式查询执行计划
    3.format=xml格式查询执行计划
    4.format=yaml格式查询执行计划
    5.清理环境
Expect      :
    1.成功
    2.输出text格式计划
    3.输出xml格式计划
    4.输出yaml格式计划
    5.成功
History     :
"""

import os
import unittest

from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger


class Explain(unittest.TestCase):

    def setUp(self):
        self.pri_sh = CommonSH('PrimaryDbUser')
        self.constant = Constant()
        self.log = Logger()
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.tb_name = 't_explain_0031'
        self.db_name = 'db_explain_0031'
        create_db = f"drop database if exists {self.db_name};" \
                    f"create database {self.db_name} dbcompatibility='B';"
        db_res = self.pri_sh.execut_db_sql(create_db)
        self.log.info(db_res)
        self.assertIn(self.constant.CREATE_DATABASE_SUCCESS, db_res,
                      f'创建兼容B库失败')

    def test_explain_sql(self):
        step1 = '-----step1:建表并插入数据;expect:成功-----'
        self.log.info(step1)
        create_tb = f'''drop table if exists {self.tb_name};
        create table {self.tb_name}(
        c_int int unique, c_varchar varchar(20), c_text text);
        insert into {self.tb_name} values(
        1, 'azAZ09—字符', lpad('abc', '50', 'a@123汉字&^%djgk'));'''
        res_createtb = self.pri_sh.execut_db_sql(create_tb, dbname=self.db_name)
        self.log.info(res_createtb)
        self.assertIn(self.constant.CREATE_TABLE_SUCCESS, res_createtb,
                      f'建表失败:{step1}')
        self.assertIn(self.constant.INSERT_SUCCESS_MSG, res_createtb,
                      f'插入数据失败:{step1}')

        step2 = '-----step2:format=text格式查询执行计划;' \
                'expect:输出text格式计划-----'
        self.log.info(step2)
        sql_text = f'''set explain_perf_mode = normal;
        explain format = text delete from {self.tb_name};
        explain format = TEXT select * from {self.tb_name};
        explain format = TeXt insert into {self.tb_name} values(
        1, 'azAZ09—字符', lpad('abc', '50', 'a@123汉字&^%djgk'));'''
        res_text = self.pri_sh.execut_db_sql(sql_text, dbname=self.db_name)
        self.log.info(res_text)
        self.assertTrue('Delete' in res_text and '[Bypass]' in res_text,
                        f'执行失败:{step2}')
        self.assertEqual(res_text.count('Seq Scan'), 2, f'执行失败:{step2}')

        step3 = '-----step3:format=xml格式查询执行计划;' \
                'expect:输出xml格式计划-----'
        self.log.info(step3)
        sql_xml = f'''set explain_perf_mode = normal;
        explain format = xml update {self.tb_name} 
        set c_varchar = 'asdf' where c_int = 1;
        explain format = XML update {self.tb_name} 
        set c_varchar = 'asdf' where c_int = 1;
        explain format = xmL update {self.tb_name} 
        set c_varchar = 'asdf' where c_int = 1;'''
        res_xml = self.pri_sh.execut_db_sql(sql_xml, dbname=self.db_name)
        self.log.info(res_xml)
        self.assertEqual(res_xml.count('<Operation>Update</Operation>'), 3,
                         f'执行失败:{step3}')
        self.assertEqual(res_xml.count('<Node-Type>Index Scan</Node-Type>'),
                         3, f'执行失败:{step3}')

        step4 = '-----step4:format=yaml格式查询执行计划;' \
                'expect:输出yaml格式计划-----'
        self.log.info(step4)
        sql_yaml = f'''set explain_perf_mode = normal;
        explain format = yaml replace into {self.tb_name} values(
        1, 'azAZ09-替换', lpad('abc', '50', 'a@123汉字&^%djgk'));
        explain format = YAML replace into {self.tb_name} values(
        1, 'azAZ09-替换', lpad('abc', '50', 'a@123汉字&^%djgk'));
        explain format = Yaml replace into {self.tb_name} values(
        1, 'azAZ09-替换', lpad('abc', '50', 'a@123汉字&^%djgk'));'''
        res_yaml = self.pri_sh.execut_db_sql(sql_yaml, dbname=self.db_name)
        self.log.info(res_yaml)
        self.assertTrue(res_yaml.count('''Node Type: "ModifyTable"         +
     Operation: "Replace"''') == 3, f'执行失败:{step4}')

    def tearDown(self):
        step5 = '-----step5:清理环境;expect:成功-----'
        self.log.info(step5)
        drop_db = self.pri_sh.execut_db_sql(f"drop database {self.db_name};")
        self.log.info(drop_db)
        self.assertIn(self.constant.DROP_DATABASE_SUCCESS, drop_db,
                      f'执行失败:{step5}')
        self.log.info(f'-----{os.path.basename(__file__)} end-----')
